﻿using Dapper;
using log4net;
using StoreProject.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StoreProject.DBUtil;

namespace StoreProject.DAL
{
    public class OrderDAL
    {
        ILog _log = LogManager.GetLogger("OrderDAL");

        public OrderDAL() 
        {
            
        }
        public IDbConnection Conn
        {
            get
            {
                return MySqlHelper.Conn;
            }
        }

        public int? Insert(OrderEntity model)
        {
            try
            {
                StringBuilder sql = new StringBuilder("INSERT INTO Orders(Id,OrderId,ProcessFlowId,LineType,FinishedProductName,SemiFinishedProductName,Batch,Customer,PlanNums,DomesticAndExportSales,RequestedDate,Operator,CreateTime,UpdateTime,JuanNum,DanZhong)");
                sql.Append(" VALUES( @Id,@OrderId,@ProcessFlowId,@LineType,@FinishedProductName,@SemiFinishedProductName,@Batch,@Customer,@PlanNums,@DomesticAndExportSales,@RequestedDate,@Operator,@CreateTime,@UpdateTime,@JuanNum,@DanZhong)");
                using (Conn)
                {
                    int insert_Result = this.Conn.Execute(sql.ToString(), model);
                    return insert_Result;
                }
            }
            catch (Exception ex)
            {
                _log.Error("Insert", ex);
                return 0;
            }
        }

        public int Update(OrderEntity model)
        {
            try
            {
                using (this.Conn)
                {
                    int update_Result = this.Conn.Update(model);
                    return update_Result;
                }
            }
            catch (Exception ex)
            {
                _log.Error("Update", ex);
                return 0;
            }
        }

        public OrderEntity Get(string id)
        {
            OrderEntity model = null;
            try
            {
                using (this.Conn)
                {
                    model = this.Conn.Get<OrderEntity>(id);
                }
            }
            catch (Exception ex)
            {
                _log.Error("Get", ex);
            }
            return model;
        }

        public bool IsExists(string id)
        {
            bool isExist = false;
            using (this.Conn)
            {
                OrderEntity model = Get(id);
                isExist = (model == null) ? false : true;
            }
            return isExist;
        }

        public int Delete(string id)
        {
            using (Conn)
            {
                OrderEntity model = new OrderEntity();
                model.Id = id;
                return Conn.Delete(model);
            }
        }

        public int Delete(OrderEntity model)
        {
            using (Conn)
            {
                return Conn.Delete(model);
            }
        }

        public IEnumerable<OrderEntity> GetList()
        {
            using (Conn)
            {
                return Conn.GetList<OrderEntity>();
            }
        }

        public IEnumerable<OrderEntity> GetListEx(string finishedProductName)
        {
            using (Conn)
            {
                string sql = string.Format("select * from Orders where FinishedProductName = '{0}'", finishedProductName);
                return Conn.Query<OrderEntity>(sql);
            }
        }

        public IEnumerable<OrderEntity> GetListByWhere(string strWhere)
        {
            using (Conn)
            {
                string sql = "select * from Orders";
                if (!string.IsNullOrEmpty(strWhere))
                {
                    sql += " where " + strWhere;
                }
                return Conn.Query<OrderEntity>(sql);
            }
        }

        
        public int DeleteAll()
        {
            using (Conn)
            {
                string sql = "delete from Orders";
                return Conn.Execute(sql);
            }
        }  
        public OrderEntity GetById(string id)
        {
            try
            {
                using (Conn)
                {
                    string sql = "select * from Orders where id=@id order by id desc ";
                    return Conn.QueryFirstOrDefault<OrderEntity>(sql, new { id = id });
                }
            }
            catch (Exception ex)
            {
                _log.Error("GetById", ex);
                return null;
            }
        }
    }
}
